package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDeliveryDTO;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.cucc.service.util.ChangeTypeUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 组装报文配送信息节点
 *
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl <br>
 */
@Service
public class BcmcPosDeliveryHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        List<KitePosDeliveryDTO> deliverys = Lists.newArrayList();

        List<Map<String, Object>> deliveryInfos = querySceneInstService.getExtParameterValue(context.getSceneInstId(),
            "offerInstDeliveryInfos");
        if (!KiteListUtils.isEmpty(deliveryInfos)) {
            deliverys.addAll(packageDeliveries(deliveryInfos));
        }

        List<Map<String, Object>> deliveryInfosDel = querySceneInstService
            .getExtParameterValue(context.getSceneInstId(), "offerInstDeliveryInfosDel");
        if (!KiteListUtils.isEmpty(deliveryInfosDel)) {
            deliverys.addAll(packageDeliveries(deliveryInfosDel));
        }

        context.getPosDto().getDelivery().addAll(deliverys);
    }

    private List<KitePosDeliveryDTO> packageDeliveries(List<Map<String, Object>> deliveryInfos) {
        List<KitePosDeliveryDTO> deliverys = Lists.newArrayList();
        for (Map<String, Object> delivery : deliveryInfos) {
            KitePosDeliveryDTO deliveryDto = new KitePosDeliveryDTO();

            deliveryDto.setInstanceId(delivery.get("instId").toString());

            List<KiteAttr> kiteAttrs = (List<KiteAttr>) delivery.get("kiteAttrs");

            deliveryDto.setIdType("5");
            deliveryDto.setIdNo(getValue(kiteAttrs, "ORD10039"));
            deliveryDto.setChangeType(ChangeTypeUtils.getChangeType(KiteMapUtils.getString(delivery, "actionType")));
            deliveryDto.setProvince(getValue(kiteAttrs, "ORD10053"));
            deliveryDto.setCity(getValue(kiteAttrs, "ORD10054"));
            deliveryDto.setDistrict(getValue(kiteAttrs, "ORD10055"));
            deliveryDto.setAddress(getValue(kiteAttrs, "ORD10040"));
            deliveryDto.setName(getValue(kiteAttrs, "ORD10037"));
            deliveryDto.setPhone(getValue(kiteAttrs, "ORD10038"));
            deliveryDto.setRemark(getValue(kiteAttrs, "ORD10041"));
            deliveryDto.setDestItemId(getValue(kiteAttrs, "extInstId"));

            deliverys.add(deliveryDto);
        }
        return deliverys;
    }

    private String getValue(List<KiteAttr> kiteAttrs, String fieldName) {
        List<KiteAttr> kiteAttr = kiteAttrs.stream()
            .filter(attr -> KiteStringUtils.equals(fieldName, attr.getFieldName())).collect(Collectors.toList());
        if (!KiteListUtils.isEmpty(kiteAttr)) {
            return kiteAttr.get(0).getValue();
        }
        return "";
    }

}
